#define _CRT_SECURE_NO_WARNINGS 1

#include <assert.h>

namespace bit
{
	template<class T>
	class vector
	{
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		iterator begin() const
		{
			return _start;
		}
		iterator end() const
		{
			return _finish;
		}
		const_iterator cbegin()
		{
			return _start;
		}
		const_iterator cend()
		{
			return _finish;
		}

		vector() = default;
		vector(int n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; ++i)
			{
				push_back(n);
			}
		}
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto e : v)
			{
				push_back(e);
			}
		}
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto e : il)
			{
				push_back(e);
			}
		}
		~vector()
		{
			if(_start)
			{
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr; 
			}

		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}
		size_t size() const
		{
			return _finish - _size;
		}
		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				int oldsize = _finish - _start;
				T* tmp = new T[n];
				if (_start)
				{
					for (int i = 0; i < size(); ++i)
					{
						tmp[i] = _start[i];
					}

					delete[] _start;
				}

				_start = tmp;
				_finish = _start + oldsize;
				_end_of_storage = _start + n;
			}
		}
		void resize(size_t n,const T& val = T())
		{
			if (n > size())
			{
				reserve(n);
				for (int i = 0; i < n; ++i)
				{
					push_back(val);
				}
			}
			else
			{
				_finish = _start + n;
			}
		}
		void push_back(const T& val)
		{
			if (_finish == _end_of_storage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}

			*_finish = val;
			++_finish;
		}
		void pop_back()
		{
			--_finish;
		}
		iterator insert(iterator pos,cosnt T& val)
		{
			assert(pos < _finish);
			assert(pos > _start);

			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;
			}

			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end - 1);
				--end;
			}

			*pos = val;
			++_finish;

			return pos;
		}
		iterator erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos > _start);

			iterator del = pos;
			while (del != _finish)
			{
				*del = (*del + 1);
				++del;
			}

			--_finish;
		}

		T& operator[](size_t pos)
		{
			return _start[pos];
		}
		T& operator[](size_t pos) const
		{
			return _start[pos];
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};
}